package com.capricornus.demo.controller;

import com.capricornus.demo.common.ApplicationException;
import com.capricornus.demo.common.ResponseResult;
import com.capricornus.demo.model.Department;
import com.capricornus.demo.model.DepartmentTree;
import com.capricornus.demo.service.user.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping
    public ResponseResult<List<DepartmentTree>> list(HttpServletRequest request, HttpServletResponse response) {


        return ResponseResult.ok(departmentService.selectDepartmentTreeList());
    }

//    @GetMapping(value = "/list")
//    public ResponseResult<List<Department>> departmentList(HttpServletRequest request, HttpServletResponse response) {
//        String deptId = request.getParameter("deptId");
//        List<Department> departmentList = departmentService.allDeptList();
//        if (deptId != "" && deptId != null){
//            for (Department department : departmentList){
//                if (deptId.equals(department.getDeptId())){
//                    department.setDisabled(true);
//                }
//            }
//        }
//        return ResponseResult.ok(departmentList);
//    }

    //获取单个部门详细信息
//    @GetMapping(value = "/deptInfo")
//    public ResponseResult<Department> deptInfo(HttpServletRequest request, HttpServletResponse response) {
//        String deptId = request.getParameter("deptId");
//        Department department = departmentService.selectDeptInfo(deptId);
//        return ResponseResult.ok(department);
//    }

//    @GetMapping(value = "/getParentDeptName")
//    public ResponseResult<Department> getParentDeptName(HttpServletRequest request, HttpServletResponse response) {
//        String parentId = request.getParameter("parentId");
//        Department department = departmentService.getDeptName(parentId);
//        return ResponseResult.ok(department);
//    }

    /**
     * 编辑部门信息
     *
     * @param department
     * @return
     * @throws Exception
     */
    @PutMapping
    public ResponseResult<String> update(@RequestBody Department department) throws Exception {
        String deptId = department.getDeptId();
        Department department1 = departmentService.selectDeptInfo(deptId);
        if (department1 == null) {
            throw new ApplicationException(String.format("部门[%s]不存在", deptId));
        }
        departmentService.update(department);
        return ResponseResult.ok("编辑部门成功");
    }

    /**
     * 新增
     *
     * @param department
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/insert")
    public ResponseResult<Object> insert(@RequestBody Department department) throws Exception {
        //生成随机ID
        if (department.getDeptId() == "") {
            department.setDeptId(UUID.randomUUID().toString());
        }

        departmentService.insert(department);
        return ResponseResult.ok("新增部门成功");
    }

    /**
     * 删除部门
     * @param departmentTree
     * @return
     * @throws Exception
     */
    @DeleteMapping(value = "/delete")
    public ResponseResult<Object> deleteDepartmentId(@RequestBody DepartmentTree departmentTree) throws Exception {
        Department department = departmentService.selectDeptInfo(departmentTree.getDeptId());
        if (department==null){
            throw new ApplicationException(String.format("部门[%s]不存在", departmentTree.getDeptId()));
        }
        departmentService.deleteDepartmentId(departmentTree);
        return ResponseResult.ok("删除成功！");
    }

    /**
     * 根据部门编码判断部门编码是否重复
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/deptCode")
    public int ResponseResult(HttpServletRequest request) throws Exception{
        String deptCode=request.getParameter("deptCode");
        int count=departmentService.selectDepartmentId(deptCode);
        return count;
    }

}
